<?php


namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\common\Selector;
use app\admin\CommonException;
use app\admin\model\Menu as MenuModel;
use think\response\Json;

class Menu extends BaseController
{
    public function index()
    {
        $model = new MenuModel();
        $list = $model->getMenus($this->request);
        $select = [
            'id' => '菜单编号',
            'name' => '菜单名称',
            'parent_id' => '父级菜单'
        ];
        $selector = Selector::render($select, 'search', $this->request->get('search', ''));
        return view('menu/index', [
            'list' => $list->toArray()['data'] ?? [],
            'page' => $list->__toString(),
            'selector' => $selector
        ]);
    }

    /**
     * @return bool|\think\response\View
     * @throws CommonException
     */
    public function add()
    {
        $data = $this->request->post();
        if (!$data) {
            return view('menu/add', ['url' => '/menu/add']);
        }

        $validate = new \app\validate\Menu();
        $result = $validate->check($data);
        if (!$result) error($validate->getError(), '', 10086);

        //新增
        $model = new MenuModel();
        $model->data($data, true);

        $res = $model->save();
        if ($res) {
            $this->success('添加成功!', '/menu/add');
        }
        error('添加失败!请稍候再试');
        return false;
    }

    /**
     * 删除
     * @throws
     */
    public function del()
    {
        $model = new MenuModel();
        $id = $this->request->get('id', '');
        if (!$id) return false;
        $menu = $model->find($id);
        if (!$menu) return false;
        return $menu->delete();
    }

    /**
     * 检查菜单名唯一
     */
    public function checkName()
    {
        $name = $this->request->post('name');
        $id = $this->request->post('id', 0);
        $validate = new \app\validate\Menu();
        $data = ['name' => $name];
        if ($id) $data['id'] = $id;
        $result = $validate->check($data);
        return $result;
    }

    /**
     * @return \think\response\View
     * @throws
     */
    public function update()
    {
        $data = $this->request->post();
        if (!$data) {
            $id = $this->request->request('id', '');
            if (!$id) throw new CommonException('缺少参数!', '/menu/index');
            $menu = MenuModel::find($id);
            if (!$menu) throw new CommonException('请刷新后重试!', '/menu/index');
            return view('menu/add', ['data' => $menu, 'url' => '/menu/update']);
        }

        //修改
        $menu = MenuModel::find($data['id']);
        if (!$menu) throw new CommonException('请刷新后重试!', '/menu/index');

        $validate = new \app\validate\Menu();
        $result = $validate->check($data);
        if (!$result) error($validate->getError(), '', 10086);

        //修改
        $menu->data($data, true);
        $res = $menu->save();
        if ($res) {
            $this->success('修改成功!', '/menu/update.html?id='.$data['id']);
        }
        error('修改失败!请稍候再试');
    }

}